package com.zadtek.jpsqishi.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zadtek.jpsqishi.R;
import com.zadtek.jpsqishi.activity.SygOrdDetailActivity;
import com.zadtek.jpsqishi.activity.SygOrdDetailActivity_;
import com.zadtek.jpsqishi.activity.common.Common;
import com.zadtek.jpsqishi.activity.common.Const;
import com.zadtek.jpsqishi.activity.data.OrderNewHelpData;
import com.zadtek.jpsqishi.views.OnChanged;

import java.util.List;

/**
 * 配送中
 *
 * @author kywwl
 */
public class OrderDeliveryHelpAdapter extends CommonAdapter<OrderNewHelpData> {
    public OrderDeliveryHelpAdapter(Context context,
                                    List<OrderNewHelpData> datas, int layoutId) {
        super(context, datas, layoutId);
        // TODO Auto-generated constructor stub
    }

    private OnChanged listener;


    @Override
    public void convert(final ViewHolders holder, final OrderNewHelpData t, int position) {
        TextView newtaskmomey = holder.getView(R.id.delivery_money);
        TextView order_number = holder.getView(R.id.order_number);
        TextView extra_reward = holder.getView(R.id.extra_reward);
        TextView newtasktime = holder.getView(R.id.delivery_time);
        LinearLayout pslay = holder.getView(R.id.help_pslay);
        TextView newtask_goods_getaddress = holder.getView(R.id.delivery_goods_getaddress);
        TextView getgpsaddr = holder.getView(R.id.getgpsaddr);
        TextView help_peisong_distance = holder.getView(R.id.help_peisong_distance);
        newtaskmomey.setText("¥" + t.getPercentage());
        order_number.setText("订单编号：" + t.getOrder_id());
        extra_reward.setText("+" + t.getExtra_reward());
        if (!t.getHelp_addtime().equals("") || t.getHelp_addtime() != null) {
            newtasktime.setText(Common.getStrTime(t.getHelp_addtime()));
        }

        ImageView newtask_order_img = holder.getView(R.id.delivery_order_img);
        TextView newtask_goods_name = holder.getView(R.id.delivery_goods_name);
        TextView newtask_goods_address = holder
                .getView(R.id.delivery_goods_address);

        final Button delivery_button = holder.getView(R.id.help_delivery_button);
        //2,配送中（确认送达），4.已送达，3.已取消，1未取货
        //状态改为 1：配送中，2：未取货，3：取消中，4：已送达，5：已取消
        if (t.getStatus().equals("7")) {
            delivery_button.setText("确认送达");
            delivery_button.setBackgroundResource(R.drawable.delivery_submit1);
        } else if (t.getStatus().equals("11")) {
            delivery_button.setText("已取消");
            delivery_button.setBackgroundResource(R.drawable.delivery_submit2);
        } else if (t.getStatus().equals("8")) {
            delivery_button.setText("已送达");
            delivery_button.setBackgroundResource(R.drawable.delivery_submit2);
        }

        delivery_button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (t.getStatus().equals("7")) {//判断与卖家距离小于200米
                    SharedPreferences sp = mContext.getSharedPreferences(Const.PREF_NAME, Context.MODE_PRIVATE);
                    String empid = sp.getString("uid", "");
                    Log.i("yudan", "empid:" + empid);
                    if (!empid.equals("") && empid != null) {
                        listener.getHelpChoiceData(t.getHelp_order_id(), empid, holder.getPositon(), t.getType());
                    }
                }
            }
        });

        LinearLayout juli_layout02 = holder.getView(R.id.juli_layout2);
        if (!t.getLocation_send().equals("0")) {
            help_peisong_distance.setText(t.getLocation_send() + "Km");
            juli_layout02.setVisibility(View.VISIBLE);
        } else {
            juli_layout02.setVisibility(View.GONE);

        }

        newtask_goods_name.setText(t.getType_get_address_name());
        newtask_goods_name.setText(t.getTitle());
        newtask_goods_address.setText(t.getGet_address());
        if (t.getGet_address().length() > 0) {
            newtask_goods_address.setVisibility(View.VISIBLE);
        } else {
            newtask_goods_address.setVisibility(View.GONE);
        }

        if (t.getType().equals("buy")) {
            newtask_order_img.setImageResource(R.drawable.gou);
        } else if (t.getType().equals("get")) {
            newtask_order_img.setImageResource(R.drawable.getgoods);
        } else if (t.getType().equals("send")) {
            newtask_order_img.setImageResource(R.drawable.fa);
        }

        try {
            String address_road = t.getMap_location();
            if (address_road == null || address_road.equals("") || address_road.equals("null")) {
                getgpsaddr.setVisibility(View.GONE);
            } else {
                getgpsaddr.setText(address_road); // 根据经纬度反查地址
                getgpsaddr.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
        }

        newtask_goods_getaddress.setText(t.getSend_address());


        pslay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (t.getHelp_order_id() != null && !t.getHelp_order_id().equals("")) {
                    Intent intent = new Intent(mContext,
                            SygOrdDetailActivity.class);
                    intent.putExtra("mainid", t.getHelp_order_id());
                    intent.putExtra("location", t.getLocation_send());
                    intent.putExtra("location_get", t.getLocation_get());
                    mContext.startActivity(intent);
                }
            }
        });

    }

    public void setHelpOnChanged(OnChanged listener) {
        this.listener = listener;
    }

}
